
<template>
  <div class="dashboard-container">
    <el-drawer ref="drawer" size="600px" :title="status==1?'新增':'编辑'" :visible.sync="showDialog" :wrapper-closable="true" @close="close">
      <div class="detial">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="仓库名称:" prop="warehouse_name">
            <el-input v-model="ruleForm.warehouse_name" placeholder="请输入仓库名称" />
          </el-form-item>
          <el-form-item label="仓库代码:" prop="warehouse_code">
            <el-input v-model="ruleForm.warehouse_code" placeholder="请输入仓库代码" />
          </el-form-item>
          <el-form-item label="仓库负责人:" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入仓库负责人" />
          </el-form-item>
          <el-form-item label="区号:" prop="area_code">
            <el-select
              v-model="ruleForm.area_code"
              style="width:100%"
              placeholder="请选择区号"
              clearable
            >
              <el-option
                v-for="(item,index) in codeList"
                :key="index"
                :label="'+' + item.area_code + item.area_name"
                :value="item.area_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="手机号:" prop="mobile">
            <el-input v-model="ruleForm.mobile" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item v-if="showArea" label="选择区域" prop="nation">
            <el-cascader
              v-model="areaList"
              filterable
              :props="codes"
              placeholder="请选择区域"
              :options="linkList"
              style="width: 100%"
              @change="changeArea()"
            />
          </el-form-item>
          <el-form-item label="详细地址:" prop="detailed">
            <el-input v-model="ruleForm.detailed" type="textarea" placeholder="请输入详细地址" />
          </el-form-item>
          <el-form-item label="邮编:" prop="postcode">
            <el-input v-model="ruleForm.postcode" placeholder="请输入邮编" />
          </el-form-item>
          <el-form-item label="状态:" prop="status">
            <el-select
              v-model="ruleForm.status"
              style="width:100%"
              placeholder="请选择状态"
              clearable
            >
              <el-option
                label="启用"
                :value="1"
              />
              <el-option
                label="禁用"
                :value="2"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="备注:" prop="desc">
            <el-input v-model="ruleForm.desc" type="textarea" placeholder="请输入备注" />
          </el-form-item>
        </el-form>
      </div>
      <div class="buttons">
        <el-button
          v-if="status == 1"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="addNews()"
        >
          新 增
        </el-button>
        <el-button
          v-if="status == 2"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="editNews()"
        >
          保 存
        </el-button>
        <el-button
          v-if="status == 1"
          size="mini"
          type=""
          @click="close()"
        >
          取消
        </el-button>

      </div>

    </el-drawer>
  </div>
</template>

<script>
import { createWarehouse, updateWarehouse } from '@/api/base/ware'
import { listMobileAreaCode } from '@/api/user'
import { countryAddressInfo } from '@/api/base/country'
import store from '@/store/modules/app.js'
import _ from 'lodash'
export default {
  name: 'Detial',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detial: {
      require: false,
      type: Object,
      default: () => {

      }
    },
    status: {
      type: Number,
      default: 1
    }
  },

  data() {
    return {
      showDialog: false,
      codes: {
        label: 'name',
        value: 'name',
        children: '_child'
      },
      ruleForm: {
        'warehouse_name': '',

        'warehouse_code': '',

        'name': '',

        'area_code': '',

        'mobile': '',

        'nation': '',

        'province': '',

        'city': '',

        'district': '',

        'detailed': '',

        'postcode': '',

        'status': '',

        'desc': ''
      },
      rules: {
        warehouse_name: [
          { required: true, message: '请输入仓库名称', trigger: 'blur' }
        ],
        warehouse_code: [
          { required: true, message: '请输入仓库代码' }
        ],
        name: [
          { required: true, message: '请输入仓库负责人' }
        ],
        area_code: [
          { required: true, message: '请选择区号' }
        ],
        mobile: [
          { required: true, message: '请输入手机号' }
        ],
        nation: [
          { required: true, message: '请选择区域' }
        ],
        detailed: [
          { required: true, message: '请输入详细地址' }
        ],
        postcode: [
          { required: true, message: '请输入邮编' }
        ],
        status: [
          { required: true, message: '请选择状态' }
        ]
      },
      areaList: [],
      codeList: [],
      linkList: [],
      showArea: false
    }
  },
  watch: {
    visible(val) {
      this.showDialog = this.visible
      if (!this.showDialog) {
        this.resetForm()
        this.showArea = false
      } else {
        this.areaList = []
        this.showArea = true
        if (this.status === 2) {
          this.ruleForm = _.cloneDeep(this.detial)
          this.ruleForm.area_code = Number(this.ruleForm.area_code)
          this.areaList = [this.ruleForm.nation, this.ruleForm.province, this.ruleForm.city, this.ruleForm.district]
        }
      }
    }
  },
  mounted() {
    this.getListMobileAreaCode()
    this.getCountryAddressInfo()
  },
  methods: {
    changeArea() {
      console.log('change', this.areaList)
      this.areaList.push('')
      this.ruleForm.nation = this.areaList[0]
      this.ruleForm.province = this.areaList[1]
      this.ruleForm.city = this.areaList[2]
      this.ruleForm.district = this.areaList[3]
    },
    getListMobileAreaCode() {
      listMobileAreaCode().then(res => {
        this.codeList = res.data.list
      })
    },
    getCountryAddressInfo() {
      this.linkList = store.state.linkList
      if (!this.linkList.length) {
        countryAddressInfo().then(res => {
          this.linkList = res.data
          this.$store.dispatch('app/setLink', res.data)
        })
      }
    },
    close() {
      this.$emit('update', false)
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.ruleForm = {
        'warehouse_name': '',

        'warehouse_code': '',

        'name': '',

        'area_code': '',

        'mobile': '',

        'nation': '',

        'province': '',

        'city': '',

        'district': '',

        'detailed': '',

        'postcode': '',

        'status': '',

        'desc': ''
      }
    },
    addNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          createWarehouse(this.ruleForm).then(res => {
            this.$message.success('添加成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    },
    editNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          updateWarehouse({
            'warehouse_id': this.ruleForm.id,
            'warehouse_name': this.ruleForm.warehouse_name,
            'warehouse_code': this.ruleForm.warehouse_code,
            'name': this.ruleForm.name,
            'area_code': this.ruleForm.area_code,
            'mobile': this.ruleForm.mobile,
            'nation': this.ruleForm.nation,
            'province': this.ruleForm.province,
            'city': this.ruleForm.city,
            'district': this.ruleForm.district,
            'detailed': this.ruleForm.detailed,
            'postcode': this.ruleForm.postcode,
            'status': this.ruleForm.status,
            'desc': this.ruleForm.desc
          }).then(res => {
            this.$message.success('编辑成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>
<style scoped>
.detial{
  padding:40px 40px 20px 20px;
}
.buttons{
  display: flex;
  flex-direction: row-reverse;
  padding-right: 40px;
}
</style>
